﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace GroupChat2013.Data
{
    /// <summary>
    /// 群消息接收类型存储类
    /// </summary>
    public class GroupNotifyRepository : BaseRepository
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public GroupNotifyRepository(string dbPath)
            : base(dbPath, "t_BubbleNotify")
        {

        }

        protected override void CreateTable()
        {
            string cmdText = @"CREATE TABLE " + TableName +
                " (Id integer primary key autoincrement,ChatId varchar(64),CreateTime datetime);";
            SQLiteHelper.ExecuteNonQuery(DBPath, cmdText);
        }

        /// <summary>
        /// 获取需要气泡提示的群ID
        /// </summary>
        /// <returns></returns>
        public List<string> GetBubbleNotifyGroups()
        {
            string cmdText = @"SELECT ChatId FROM " + TableName;

            List<string> result = new List<string>();
            var cmd = SQLiteHelper.CreateCommand(DBPath, cmdText);
            using (IDataReader reader = SQLiteHelper.ExecuteReader(cmd, cmdText, null))
            {
                while (reader.Read())
                {
                    result.Add(reader.GetString(reader.GetOrdinal("ChatId")));
                }
            }
            return result;
        }

        /// <summary>
        /// 添加需要气泡提示的群ID
        /// </summary>
        /// <param name="recLoginInfo"></param>
        public void AddBubbleNotifyGroup(string groupId)
        {
            if (string.IsNullOrEmpty(groupId))
            {
                return;
            }

            if (IsExistGroup(groupId))
                return;

            string cmdText = @"INSERT INTO " + TableName + @" (ChatId, CreateTime) 
                                VALUES (@ChatId,@CreateTime)";

            SQLiteHelper.ExecuteNonQuery(DBPath, cmdText,
                                         groupId,
                                         DateTime.Now);
        }

        /// <summary>
        /// 删除气泡提示群
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public void DeleteGroup(string groupId)
        {
            if (IsExistGroup(groupId))
            {
                string cmdText = "DELETE FROM " + TableName + " where ChatId=@chatId ";
                SQLiteHelper.ExecuteNonQuery(DBPath, cmdText, groupId);
            }
        }

        /// <summary>
        /// 是否存在气泡提示群
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool IsExistGroup(string groupId)
        {
            string cmdText = "SELECT count(*) FROM " + TableName + " where ChatId=@chatId ";
            object data = SQLiteHelper.ExecuteScalar(DBPath, cmdText, groupId);
            return Convert.ToInt32(data) > 0;
        }
    }
}
